<?php
set_time_limit(0);
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past

require_once("config.php");
require_once("functions.php");

if (!$allow_url_fopen) {
	echo '<span class="error"><b>Note</b>: Please enable \'<em>allow_url_fopen</em>\' option in PHP configuration file</span><br/>';
}

require_once("MSN.php");

$g = new MSN();

$temp_pageurls = $_REQUEST['pageurls'];
$temp_keywords = $_REQUEST['keywords'];
$num = intval(@$_REQUEST['num']);

$pageurls = array();
$keywords = array();

if (empty($num)) $num = 1;

$temp = explode('~@~',$temp_pageurls);

foreach($temp as $val) {

	if (!empty($val)) {
		$pageurls[] = $val;
	}
}

$temp = explode('~@~',$temp_keywords);

foreach($temp as $val) {

	if (!empty($val)) {
		$keywords[] = $val;
	}
}

$results = array();
$count = 0;
$js_array = 'var collection= new Array();'."\n";

if ( !empty($pageurls) && !empty($keywords) ) {

	foreach ( $keywords as $keyword ) {

		$first = ($num-1) * 100 + 1;
		$pr = $first;

		for ($i=0; $i<10; $i++) {

			if (isset($_SESSION[$keyword]) )
				if ( $first-10 > $_SESSION[$keyword] ) break;

			list($total, $links) = $g->get_links(urlencode($keyword)."&first=".$first);

			$_SESSION[$keyword] = $total;

			
//echo "<pre>"; print_r($links);
			$old_pr = $pr;

			foreach ( $pageurls as $url ) {
				
				$pr = $old_pr;

				foreach ($links as $link) {

					$pr++;

					if (strpos($link['link'], $url) !== false) {

						$results[$count]['link'] = $link['link'];
						$results[$count]['url'] = $url;
						$results[$count]['key'] = $keyword;
						$results[$count++]['pr'] = $pr;
					}

				} //foreach
			}

			$first += 10;
			$pr = $first;
		} //for
	}
}


$k = 1;
foreach ($results as $details ) {
?>
  <tr class="tr<?php echo (($k % 2 == 0)?'1':'2');  ?>">
    <td align="left"><?php echo $details['url']; ?></td>
    <td align="left"><?php echo $details['key']; ?></td>
	<td align="center"><?php echo $details['pr']; ?></td>
	<td align="left"><?php echo $details['link']; ?></td>
  </tr>
<?php
}
?>